<%@ page pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
<script type="text/javascript">
	$('#admin-tt').datagrid({ 
		pagination:true,
		remoteSort:false, 
		nowrap:false, 
		fitColumns:true, 
		url:'${pageContext.request.contextPath}/admin/queryAdmin', 
		toolbar:[{	
					text:'添加管理员',
					iconCls: 'icon-add',
					handler: function(){
						$("#addAdmin").dialog({    
							    title: '添加管理员',    
							    width: 400,    
							    height:400,    
							    closed: false,    
							    cache: false,    
							    href: '${pageContext.request.contextPath}/admin/register.jsp',    
							    modal: true   
							}); 
					}
				},{
					text:'删除管理员',
					iconCls: 'icon-remove',
					handler: function(){
					var id=$("#admin-tt").datagrid("getSelected").id;
						$.messager.confirm(
	  						"系统提示？",
	  						"确认删除吗？",
	  						function(fn){
	  							$.post(
	  								"${pageContext.request.contextPath}/admin/deleteAdmin",
	  								{"id":id},
	  								function(){
	  									$("#admin-tt").datagrid("reload")
	  								}
	  							);
	  						}
  						)
					}
			}],
		columns:[[
				{field:'admin_id',title:'ID',width:1,align:'center'}, 
				{field:'username',title:'用户名',width:1,sortable:true,align:'center'}, 
				{field:'password',title:'密码',width:1,align:'center',sortable:true}, 
				{field:'power',title:'权限',sortable:true,align:'center',
						formatter:function(value,row,index){
						//console.log(value,row,index)
								if (value==1){
									return '<input value ='+row.admin_id+' class="adminbutton" checked style="width:100px;height:30px">';
								} else {
									return '<input value ='+row.admin_id+' class="adminbutton" style="width:100px;height:30px">';
								}
							}
				}
			]], 
		 view: detailview, 
		 onLoadSuccess:function(data){
			$(".adminbutton").switchbutton({ 
					onText:"超级管理员",
					offText:"普通管理员",
			        onChange: function(checked){
			       //console.log(checked)
			        	var adminid=$(this).val();
			        	console.log(adminid)
			        	var power = 0;
			        	if(checked)
			        	power = 1;
						$.post(
								"${pageContext.request.contextPath}/admin/updatePower",
								{"power":power,"id":adminid},
								function(){
									$('#p-admin'+adminid).text(power==1?'权限: 超级管理员':'权限: 普通管理员')
								}
							);	
				      } 
				 });
			},
		detailFormatter:function(rowIndex, rowData){
			//console.log(rowData) 
			var power=rowData.power==1?'超级管理员':'普通管理员';
			return '<table><tr>' + 
			'<td rowspan=2 style="border:0"><img src="image/' + rowData.img_path + '" style="height:50px;"></td>' + 
			'<td style="border:0">' + 
			'<p>用户名: ' + rowData.username + '</p>' + 
			'<p id="p-admin'+rowData.admin_id+'">权限: ' + power + '</p>' + 
			'</td>' + 
			'</tr></table>'; 
		} 
	});
	
</script>
<div id="addAdmin"></div>
<div id="updateAdminpassword"></div>
<table id="admin-tt"></table> 